﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;

namespace MainFrmTool.ToolBox
{
    public class ToolBoxGroup:ToolBoxItem
    {
        public bool IsOpen = false;
        public List<ToolBoxItem> Childs = new List<ToolBoxItem>();
        public ToolBoxGroup():base()
        {
           
        }
        public void AppendItem(ToolBoxItem item) 
        {
            this.Childs.Add(item);
        }
        public void RemoveItem(ToolBoxItem item) 
        {
            this.Childs.Remove(item);
        }
        public ToolBoxItem Contain(Point p) 
        {
            if (this.DrawRect.Contains(p)) 
            {
                return this;
            }
            if (IsOpen) 
            {
                foreach (var item in Childs)
                {
                    if (item.DrawRect.Contains(p)) 
                    {
                        return item;
                    }
                }
            }
            return null;
        }

        public int GetHeight() 
        {
            if (IsOpen) 
            {
                return 20 + 20 * Childs.Count;
            }
            return 20;
        }

        public override void Draw(Graphics g, ref int Y,int width)
        {
            DrawRect.Width = width;
            DrawText.Width = width - 20;
            DrawRect.Y = Y;
            DrawText.Y = Y;
            Y += DrawRect.Height;
            if (IsSelect) 
            {
                g.FillRectangle(ToolBoxDraw.SelectBrush, DrawRect);
                g.DrawString(this.Text, SystemFonts.CaptionFont, Brushes.White, DrawText, ToolBoxDraw.TextAlign);
            }
            else if (IsHover)
            {
                g.FillRectangle(Brushes.White, DrawRect);
                g.DrawString(this.Text, SystemFonts.CaptionFont, Brushes.Black, DrawText, ToolBoxDraw.TextAlign);
            }
            else 
            {
                g.DrawString(this.Text, SystemFonts.CaptionFont, Brushes.Black, DrawText, ToolBoxDraw.TextAlign);
            }
            DrawTrigon(g);
            if (this.IsOpen) 
            {
                foreach (var item in Childs)
                {
                    item.Draw(g, ref Y,width);
                }
            }
        }

        private void DrawTrigon(Graphics g)
        {
            if (IsOpen)
            {
                g.DrawImage(ToolBoxDraw.OpenImg, 4, DrawRect.Y + 4,12,12);   
            }
            else 
            {
                g.DrawImage(ToolBoxDraw.CloseImg, 4, DrawRect.Y +4,12,12);  
            }
        }
    }
}
